package com.black.app.service.impl.find;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.find.AppFindZhangzhishiComments;
import com.black.app.domain.req.find.add.ZhangzhishiCommentsaddreq;
import com.black.app.domain.req.find.query.AppFindZhangzhishicommentreq;
import com.black.app.mapper.find.AppFindZhangzhishiCommentsMapper;
import com.black.app.service.find.AppFindZhangzhishiCommentsService;
import com.black.common.constant.CacheConstants;
import com.black.common.exception.ServiceException;
import com.black.common.exception.database.PrimaryException;
import com.black.common.exception.database.RecordNotFoundException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author 30223
* @description 针对表【app_find_zhangzhishi_comments(涨知识-文章评论表)】的数据库操作Service实现
* @createDate 2024-10-25 11:59:26
*/
@Service
public class AppFindZhangzhishiCommentsServiceImpl extends ServiceImpl<AppFindZhangzhishiCommentsMapper, AppFindZhangzhishiComments>
    implements AppFindZhangzhishiCommentsService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageInfo<AppFindZhangzhishiComments> paginationQuery(AppFindZhangzhishicommentreq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    @Override
    public boolean add(ZhangzhishiCommentsaddreq req) {
        //验证评论id和评论人id是否重复
        if(this.baseMapper.exists(new LambdaQueryWrapper<AppFindZhangzhishiComments>().eq(AppFindZhangzhishiComments::getCommentsuserid,req.getCommentsuserid()))){
            throw new ServiceException("评论人id重复");
        }

        AppFindZhangzhishiComments entity = new AppFindZhangzhishiComments();
        BeanUtils.copyProperties(req, entity);
        boolean save = this.save(entity);
        if (save){
            redisTemplate.opsForValue().set(CacheConstants.APP_FIND_ZHANGZHISHI_KEY+req.getCommentsuserid(),entity);
        }
        return save;
    }

    @Override
    public boolean deleteById(Long id) {
        if (Objects.isNull(id)) {
            throw new PrimaryException();
        }

        AppFindZhangzhishiComments entity = this.getById(id);

        if (Objects.isNull(entity)) {
            throw new RecordNotFoundException();
        }
        return this.removeById(id);
    }
}




